/**
 * function submitform(key)
 *             {                
 *                 document.getElementById("loaiphong").value = key;
 *                 alert(key);
 *                 //document.forms['myform'].submit();
 *             };
 */

function Check(key) {
    var one_day=1000*60*60*24;
    var ngay = parseInt(document.getElementById("ngay").value); //0
    var thang = parseInt(document.getElementById("thang").value); //0
    var nam = parseInt(document.getElementById("nam").value); //0
    var songay = parseInt(document.getElementById("songay").value); //0

    var ngaybatdau = parseInt(document.getElementById("ngaybatdau").value); //0
    var thangbatdau = parseInt(document.getElementById("thangbatdau").value); //0
    var nambatdau = parseInt(document.getElementById("nambatdau").value); //0\

    var ngayketthuc = parseInt(document.getElementById("ngayketthuc").value); //0
    var thangketthuc = parseInt(document.getElementById("thangketthuc").value); //0
    var namketthuc = parseInt(document.getElementById("namketthuc").value); //0\

    var n = parseInt(document.getElementById("soloaiphong").value);;
    if (document.getElementById("deal" + key).checked == true) {
        if (ngaybatdau == 0) {
            var class_tab = "deals select-col1 ";
            class_tab += " select" + key;
            for (var i = 0; i < n; i++) {
                var o = document.getElementById("tab_book_" + i);
                o.className = class_tab;
            }
            ngay = ngay + key;
            if (ngay > songay) {
                ngay = ngay - songay;
                thang = thang + 1;
                if (thang > 12) {
                    thang = 1;
                    nam = nam + 1;
                }
            }
            document.getElementById("ngaybatdau").value = ngay;
            document.getElementById("thangbatdau").value = thang;
            document.getElementById("nambatdau").value = nam;
            document.getElementById("ngayketthuc").value = ngay;
            document.getElementById("thangketthuc").value = thang;
            document.getElementById("namketthuc").value = nam;
        }
        else {            
            var startdate = new Date(nam, thang-1, ngay);
            startdate = startdate.getTime();

            var keyngay = ngay + key;
            var keythang = thang;
            var keynam = nam;
            if (keyngay > songay) {
                keyngay = keyngay - songay;
                keythang = thang + 1;
                if (keythang > 12) {
                    keythang = 1;
                    keynam = nam + 1;
                }
            }

            var keydate = new Date(keynam, keythang-1, keyngay);
            keydate = keydate.getTime();

            var ngay2 = ngay + 14;
            var thang2 = thang;
            var nam2 = nam;
            if (ngay2 > songay) {
                ngay2 = ngay2 - songay;
                thang2 = thang + 1;
                if (thang2 > 12) {
                    thang2 = 1;
                    nam2 = nam + 1;
                }
            }

            var enddate = new Date(nam2, thang2-1, ngay2);
            enddate = enddate.getTime();

            var datebd = new Date(nambatdau, thangbatdau-1, ngaybatdau);
            datebd = datebd.getTime();
            var datekt = new Date(namketthuc, thangketthuc-1, ngayketthuc);
            datekt = datekt.getTime();
            if (keydate > datekt) {
                if (datekt < startdate || (datekt >= startdate && datebd<startdate)) {
                    var class_tab = "deals select-col1 ";
                    for (var i = 0; i <= key; i++) {
                        class_tab += " select" + i;
                        document.getElementById("deal" + i).checked = true;
                        if (i > 0 && i < key)
                            document.getElementById("deal" + i).disabled = true;
                    }
                    for (var i = 0; i < n; i++) {
                        o = document.getElementById("tab_book_" + i);
                        o.className = class_tab;

                    }
                    document.getElementById("ngayketthuc").value = keyngay;
                    document.getElementById("thangketthuc").value = keythang;
                    document.getElementById("namketthuc").value = keynam;
                }
                else {
                    var vitribd = Math.ceil((datebd-startdate)/(one_day));
                    var class_tab = "deals select-col1 ";
                    for (var i = vitribd; i <= key; i++) {
                        class_tab += " select" + i;
                        document.getElementById("deal" + i).checked = true;
                        if (i > vitribd && i < key)
                            document.getElementById("deal" + i).disabled = true;
                    }
                    for (var i = 0; i < n; i++) {
                        o = document.getElementById("tab_book_" + i);
                        o.className = class_tab;
                    }
                    document.getElementById("ngayketthuc").value = keyngay;
                    document.getElementById("thangketthuc").value = keythang;
                    document.getElementById("namketthuc").value = keynam;
                }
            }
            else if (keydate < datebd) {
                if (datebd >= enddate || (datebd < enddate && datekt >= enddate)) {
                    var class_tab = "deals select-col1 ";
                    for (var i = key; i < 14; i++) {
                        class_tab += " select" + i;
                        document.getElementById("deal" + i).checked = true;
                        if (i > key && i < 13)
                            document.getElementById("deal" + i).disabled = true;
                    }
                    for (var i = 0; i < n; i++) {
                        o = document.getElementById("tab_book_" + i);
                        o.className = class_tab;
                    }
                    document.getElementById("ngaybatdau").value = keyngay;
                    document.getElementById("thangbatdau").value = keythang;
                    document.getElementById("nambatdau").value = keynam;
                }
                else {
                    var class_tab = "deals select-col1 ";
                    var vitrikt = Math.ceil((datekt-startdate)/(one_day));
                    for (var i = key; i <= vitrikt; i++) {
                        class_tab += " select" + i;
                        document.getElementById("deal" + i).checked = true;
                        if (i > key && i < vitrikt)
                            document.getElementById("deal" + i).disabled = true;
                    }
                    for (var i = 0; i < n; i++) {
                        o = document.getElementById("tab_book_" + i);
                        o.className = class_tab;
                    }
                    document.getElementById("ngaybatdau").value = keyngay;
                    document.getElementById("thangbatdau").value = keythang;
                    document.getElementById("nambatdau").value = keynam;
                }
            }
        }
    }else{
        var startdate = new Date(nam, thang, ngay);
        startdate = startdate.getTime();

            var keyngay = ngay + key;
            var keythang = thang;
            var keynam = nam;
            if (keyngay > songay) {
                keyngay = keyngay - songay;
                keythang = thang + 1;
                if (keythang > 12) {
                    keythang = 1;
                    keynam = nam + 1;
                }
            }

            var keydate = new Date(keynam, keythang, keyngay);
            keydate = keydate.getTime();

            var ngay2 = ngay + 14;
            var thang2 = thang;
            var nam2 = nam;
            if (ngay2 > songay) {
                ngay2 = ngay2 - songay;
                thang2 = thang + 1;
                if (thang2 > 12) {
                    thang2 = 1;
                    nam2 = nam + 1;
                }
            }

            var enddate = new Date(nam2, thang2, ngay2);
            enddate = enddate.getTime();

            var datebd = new Date(nambatdau, thangbatdau, ngaybatdau);
            datebd = datebd.getTime();
            var datekt = new Date(namketthuc, thangketthuc, ngayketthuc);
            datekt = datekt.getTime();
        if(datebd == datekt)
        {
            var class_tab = "deals select-col1 ";
            for (var i = 0; i < n; i++) {
                var o = document.getElementById("tab_book_" + i);
                o.className = class_tab;
            }
            document.getElementById("ngaybatdau").value = 0;
            document.getElementById("thangbatdau").value = 0;
            document.getElementById("nambatdau").value = 0;
            document.getElementById("ngayketthuc").value = 0;
            document.getElementById("thangketthuc").value = 0;
            document.getElementById("namketthuc").value = 0;
        }
        else
        {
            if(keydate==datebd || key==0)
            {
                var ngay1 = keyngay + 1;
                var thang1 = keythang;
                var nam1 = keynam;
                if (ngay1 > songay) {
                    ngay1 = ngay1 - songay;
                    thang1 = thang1 + 1;
                    if (thang1 > 12) {
                        thang1 = 1;
                        nam1 = nam + 1;
                    }
                }
                for (var i = 0; i < n; i++) {
                    o = document.getElementById("tab_book_" + i);
                    removeClass(o, "select"+key);
                }
                key = key + 1;
                if(key < 13)
                    document.getElementById("deal" + key).disabled = false;
                document.getElementById("ngaybatdau").value = ngay1;
                document.getElementById("thangbatdau").value = thang1;
                document.getElementById("nambatdau").value = nam1;
            }
            else if(keydate==datekt || key==13)
            {
                var ngay1 = keyngay - 1;
                var thang1 = keythang;
                var nam1 = keynam;
                if (ngay1 == 0) {
                    thang1 = thang1 - 1;
                    if (thang1 == 0) {
                        thang1 = 12;
                        nam1 = nam - 1;
                    }
                    var songay1 = DaysInMonth(nam1,thang1);
                    ngay1 = songay1;
                }
                for (var i = 0; i < n; i++) {
                    o = document.getElementById("tab_book_" + i);
                    removeClass(o, "select"+key);
                }
                key = key - 1;
                if(key >= 0)
                    document.getElementById("deal" + key).disabled = false;
                document.getElementById("ngayketthuc").value = ngay1;
                document.getElementById("thangketthuc").value = thang1;
                document.getElementById("namketthuc").value = nam1;
            }
        }                    
    }
    ngaybatdau = parseInt(document.getElementById("ngaybatdau").value); //0
    thangbatdau = parseInt(document.getElementById("thangbatdau").value); //0
    nambatdau = parseInt(document.getElementById("nambatdau").value); //0\

    ngayketthuc = parseInt(document.getElementById("ngayketthuc").value); //0
    thangketthuc = parseInt(document.getElementById("thangketthuc").value); //0
    namketthuc = parseInt(document.getElementById("namketthuc").value); //0\
    datebd = new Date(nambatdau, thangbatdau-1, ngaybatdau);
    datebd = datebd.getTime();
    datekt = new Date(namketthuc, thangketthuc-1, ngayketthuc);
    datekt = datekt.getTime();
    var sodemo = Math.ceil((datekt-datebd)/(one_day));
    document.getElementById("sodemo").value = sodemo + 1;
}

function load_() {
    document.getElementById("ngaybatdau").value = 0;
    document.getElementById("thangbatdau").value = 0;
    document.getElementById("nambatdau").value = 0;
    document.getElementById("ngayketthuc").value = 0;
    document.getElementById("thangketthuc").value = 0;
    document.getElementById("namketthuc").value = 0;
    for (var i = 0; i < 14; i++)
        document.getElementById("deal" + i).checked = false;
    for (var i = 0; i < 14; i++)
        document.getElementById("deal" + i).disabled = false;
}

function load_chitietdat(key) {
    document.getElementById("numberRooms").value = 1;    
    for(var i = 1;  i < 11; i++)
    {
        document.getElementById("room"+i+"NumberAdults").value = parseInt(key);
        document.getElementById("room"+i+"NumberChildren").value = 0;
        document.getElementById("room"+i+"bedding1").checked= false;
        document.getElementById("room"+i+"bedding2").checked= false;
    }
    
}

function DaysInMonth(y,m) { return new Date(y,m,0).getDate(); }

function hasClass(el, name) {
   return new RegExp('(\\s|^)'+name+'(\\s|$)').test(el.className);
} 

function addClass(el, name)
{
   if (!hasClass(el, name)) { el.className += (el.className ? ' ' : '') +name; }
}

function removeClass(el, name)
{
   if (hasClass(el, name)) {
      el.className=el.className.replace(new RegExp('(\\s|^)'+name+'(\\s|$)'),' ').replace(/^\s+|\s+$/g, '');
   }
}

function showInfoPlayBackOffice()
{
    var o = document.getElementById("link_office_ivivu");
    addClass(o, "active");
    var o = document.getElementById("link_abc_bank");
    removeClass(o, "active");
    var o = document.getElementById("link_bank_transfer");
    removeClass(o, "active");
    var o = document.getElementById("office_ivivu");
    o.style.display = "block";
    var o = document.getElementById("abc_bank");
    o.style.display = "none";
    var o = document.getElementById("bank_transfer");
    o.style.display = "none";
}

function showInfoPlayBackABC()
{
    var o = document.getElementById("link_office_ivivu");
    removeClass(o, "active");
    var o = document.getElementById("link_abc_bank");
    addClass(o, "active");
    var o = document.getElementById("link_bank_transfer");
    removeClass(o, "active");
    var o = document.getElementById("office_ivivu");
    o.style.display = "none";
    var o = document.getElementById("abc_bank");
    o.style.display = "block";
    var o = document.getElementById("bank_transfer");
    o.style.display = "none";
}

function showInfoPlayBackTranfer()
{
    var o = document.getElementById("link_office_ivivu");
    removeClass(o, "active");
    var o = document.getElementById("link_abc_bank");
    removeClass(o, "active");
    var o = document.getElementById("link_bank_transfer");
    addClass(o, "active");
    var o = document.getElementById("office_ivivu");
    o.style.display = "none";
    var o = document.getElementById("abc_bank");
    o.style.display = "none";
    var o = document.getElementById("bank_transfer");
    o.style.display = "block";
}
